package com.tools.payhelper.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    public static  final  long DAY_TIME_STAMP = 60 * 60 * 24 * 1000;
    public static final String FORMAT_YMD = "yyyy-MM-dd";
    public static final String FORMAT_HMS1 = "HH-mm-ss";
    //yyyymmddhhiiss
    public static final String FORMAT_yyyyMMddHHmmss = "yyyyMMddHHmmss";

    public static String getData(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date(time));
    }
    // 日期
    private static String getDayTime(long time) {
        return new SimpleDateFormat(FORMAT_YMD).format(new Date(time));
    }
    // 今天日期
    public static String getTodayTime() {
        return new SimpleDateFormat(FORMAT_YMD).format(new Date(System
                .currentTimeMillis()));
    }

    /**
     * HHmmss
     *
     * @param time
     * @return
     */
    public static String getTime3(long time) {
        return new SimpleDateFormat(FORMAT_HMS1).format(new Date(time));
    }
    /**
     * yyyyMMddHHmmss
     *
     * @return
     */
    public static String getFORMAT_yyyyMMddHHmmss() {
        return new SimpleDateFormat(FORMAT_yyyyMMddHHmmss).format(new Date(System.currentTimeMillis()));
    }
    /**
     * 获取带时分秒 的标准日期格式
     * @param date
     * @return
     */
    public static String getDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }


    public static String getTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(time));
    }
    public static String getYMD(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
        return format.format(new Date(time));
    }


    public static String getHourAndMin(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(new Date(time));
    }

    public static String getChatTime(long timesamp) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date today = new Date(System.currentTimeMillis());
        Date otherDay = new Date(timesamp);
        int temp =
                Integer.parseInt(sdf.format(today))
                        - Integer.parseInt(sdf.format(otherDay));

        switch (temp) {
            case 0:
                result = "今天 " + getHourAndMin(timesamp);
                break;
            case 1:
                result = "昨天 " + getHourAndMin(timesamp);
                break;
            case 2:
                result = "前天 " + getHourAndMin(timesamp);
                break;

            default:
                result = getTime(timesamp);
                break;
        }
        return result;
    }


    public static String getChatTimeStyle(long timesamp) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date today = new Date(System.currentTimeMillis());
        Date otherDay = new Date(timesamp);
        int temp =
                Integer.parseInt(sdf.format(today))
                        - Integer.parseInt(sdf.format(otherDay));

        switch (temp) {
            case 0:
                result = getHourAndMin(timesamp);
                break;
            case 1:
                result = "昨天 " + getHourAndMin(timesamp);
                break;
            case 2:
                result = getTime(timesamp);
                break;

            default:
                result = getTime(timesamp);
                break;
        }
        return result;
    }

    /**
     * @方法名: twoDateDistance
     * @描述: 比较两个日期相差几天
     * @设定: @param startDate
     * @设定: @param endDate
     * @设定: @return 设定文件
     * @返回: long 返回类型
     * @日期: 2014-7-8 下午3:09:31
     * @throws
     */
    public static long twoDateDistance(long startDate, long endDate) {
        long timeLong = endDate - startDate;
        if (timeLong > 60 * 60 * 24 * 1000) {
            timeLong = timeLong / 1000 / 60 / 60 / 24;
            return timeLong;
        }

        // if (timeLong < 60 * 60 * 1000) {
        // timeLong = timeLong / 1000 / 60;
        // Logger.d(timeLong + "分钟前");
        // return timeLong;
        // }
        return -1;
    }

    /**
     * @Description 指定年月的最后一天日期
     * @param year
     * @param month
     * @param simpleDateFormat
     * @return String
     */
    public static String getMonthLastDayDate(int year, int month,
                                             SimpleDateFormat simpleDateFormat) {
        if (year < 0) return null;
        Calendar cal = Calendar.getInstance();
        if (month < 0) {
            cal.set(Calendar.YEAR, year - 1);
            cal.set(Calendar.MONTH, 12 + month % 11);
        } else if (month > 11) {
            cal.set(Calendar.YEAR, year + 1);
            cal.set(Calendar.MONTH, month % 11);
        } else {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month);
        }
        cal.set(Calendar.DAY_OF_MONTH,
                cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String date = simpleDateFormat.format(cal.getTime());
        return date;
    }

    /**
     * @Description 指定年月的第一天日期
     * @param year
     * @param month
     * @param simpleDateFormat
     * @return String
     */
    public static String getMonthFirstDayDate(int year, int month,
                                              SimpleDateFormat simpleDateFormat) {
        if (year < 0) return null;
        Calendar cal = Calendar.getInstance();

        if (month < 0) {
            cal.set(Calendar.YEAR, year - 1);
            cal.set(Calendar.MONTH, 12 + month);
        } else if (month > 11) {
            cal.set(Calendar.YEAR, year + 1);
            cal.set(Calendar.MONTH, month % 11);
        } else {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month);
        }
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String date = simpleDateFormat.format(cal.getTime());
        return date;
    }

    /**
     * @Description: 指定日期当周第一天或最后一天
     * @param cal
     * @param flag
     * @return：String
     */
    public static String getFirstLastWeek(Calendar cal, boolean flag) {
        // flag true周第一天 false 周最后一天

        int dw = cal.get(Calendar.DAY_OF_WEEK);
        if (!flag) cal.setTimeInMillis(cal.getTimeInMillis() + (7 - dw) * 24
                * 60 * 60 * 1000);
        else cal.setTimeInMillis(cal.getTimeInMillis() - (dw - 1) * 24 * 60
                * 60 * 1000);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        String showTime = formatter.format(cal.getTime());
        return showTime.toString();
    }

    /**
     * @Description: 指定日期当月第一天或最后一天
     * @param cal
     * @param flag
     * @return：String
     */
    public static String getFirstLastMoonth(Calendar cal, boolean flag) {
        // flag true月第一天 false 月最后一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        if (!flag) {
            cal.roll(Calendar.DAY_OF_MONTH, -1);
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String showTime = formatter.format(cal.getTime());
        return showTime.toString();
    }

    /**
     * @Description: 指定日期string格式
     * @param cal
     * @return：String
     */
    public static String getDateString(Calendar cal) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String showTime = formatter.format(cal.getTime());
        return showTime.toString();
    }
    /**
     * 获取毫秒值
     */

    public static long getTimeStamp(String time) throws  Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(time);
        return date.getTime();

    }
    public static String getTimeMMdd(String time) throws  Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(time);

        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        return format.format(date.getTime());

    }
}
